package com.ruoyi.file.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.file.domain.FileDO;
import com.ruoyi.file.domain.vo.file.FileCreateReqVO;
import com.ruoyi.file.domain.vo.file.FileRespVO;
import com.ruoyi.file.domain.vo.file.FileUploadReqVO;
import com.ruoyi.file.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import org.apache.commons.io.IOUtils;
import javax.validation.Valid;
import java.util.List;

@Tag(name = "管理后台 - 文件存储")
@RestController
@RequestMapping("/infra/file")
@Validated
@Slf4j
public class FileController {

    @Resource
    private FileService fileService;

    @PostMapping("/upload")
    @Operation(summary = "上传文件", description = "模式一：后端上传文件")
    public AjaxResult uploadFile(@ModelAttribute FileUploadReqVO uploadReqVO) {

        return AjaxResult.success(fileService.createFile(uploadReqVO));
    }

    @GetMapping("/notice/images/{noticeId}")
    @Operation(summary = "获取公告的图片信息")
    @Parameter(name = "noticeId", description = "公告编号", required = true)
    public AjaxResult getNoticeImages(@PathVariable("noticeId") Long noticeId) {
        List<FileRespVO> images = fileService.getNoticeImages(noticeId);
        return AjaxResult.success(images);
    }

    @PutMapping("/submitImageIds")
    @Operation(summary = "修改对应file_id")
    public AjaxResult submitImageIds(@RequestParam Long noticeId, @RequestParam List<Long> imageIds) {
        fileService.submitImageIds(noticeId, imageIds);
        return AjaxResult.success();
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除文件")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('infra:file:delete')")
    public AjaxResult deleteFile(@RequestParam("id") Long id) throws Exception {
        fileService.deleteFile(id);
        return AjaxResult.success(true);
    }

//    @GetMapping("/{configId}/get/**")
//    @PermitAll
//    @Operation(summary = "下载文件")
//    @Parameter(name = "configId", description = "配置编号", required = true)
//    public void getFileContent(HttpServletRequest request,
//                               HttpServletResponse response,
//                               @PathVariable("configId") Long configId) throws Exception {
//        // 获取请求的路径
//        String path = StrUtil.subAfter(request.getRequestURI(), "/get/", false);
//        if (StrUtil.isEmpty(path)) {
//            throw new IllegalArgumentException("结尾的 path 路径必须传递");
//        }
//        // 解码，解决中文路径的问题 https://gitee.com/zhijiantianya/ruoyi-vue-pro/pulls/807/
//        path = URLUtil.decode(path);
//
//        // 读取内容
//        byte[] content = fileService.getFileContent(configId, path);
//        if (content == null) {
//            log.warn("[getFileContent][configId({}) path({}) 文件不存在]", configId, path);
//            response.setStatus(HttpStatus.NOT_FOUND.value());
//            return;
//        }
//        writeAttachment(response, path, content);
//    }


}
